var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        // IsAccept:同意,IsReject:拒绝,IsRevoke:撤销,
        list: [],
        form: {
            pageindex: 0,
            pagesize: 10,
            keywords:'',
            status: 1, //状态（1-待处理 2-已处理 3-我发起的 4-我收到的）
            type:0,
        },
        current_item_length:0,
        total: {
            PendingNumber: 0,//待处理数量
            AcceptNumber: 0,//已处理
            StartNumber: 0,//我发起的
            SendNumber: 0,//我收到的
        },
        typeList:[],
        can_scroll: false,
        showsearch:false,
        show:false,//分类筛选
        chooseTypeid:0,//选中分类id
        checked:false,//是否仅查看未读
        Read:false,
    },
    methods: {
        get_data: function () {
            var that = this;
            that.can_scroll = false;
            var options = {};
            options.Keywords = that.form.keywords;
            options.PageSize = that.form.pagesize;
            options.PageIndex = that.form.pageindex;
            options.NavState = that.form.status;
            options.Read = that.Read;
            options.Type = that.form.type;
            options.action = "getapprovallist";
            console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                    console.log(JSON.stringify(err));
                if (succeed) {
                    
                    for (let i = 0; i < data.data.length; i++) {
                        data.data[i].AddTime=ns.SetTime_style(data.data[i].AddTime);
                    }
                    if (data.data.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.data;
                    } else {
                        that.list = that.list.concat(data.data);
                    }
                    app.current_item_length = that.list.length;
                    that.total.PendingNumber=data.summary.PendingNumber;
                    that.total.AcceptNumber=data.summary.AcceptNumber;
                    that.total.StartNumber=data.summary.StartNumber;
                    that.total.SendNumber=data.summary.SendNumber;
 
                } else if (err) {
                    that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        //获取类型列表
        get_menulist:function(){
            var that=this;
            var options={};
            options.NavState=that.form.status;
            options.Keywords=that.form.keywords;
            options.action='getapprovaltypetotal';
            ns.post(options, function (succeed, data, err){
                if(succeed){
                    if(data.status){
                        that.typeList=data.data;
                    }
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })

        },

        do_open_search:function(){
            var that=this;
            that.show=!that.show;
        },
        choose_noread:function(){
            var that=this;
            that.Read=!that.Read;
            that.get_data();
            // that.checked=!that.checked;
            // console.log(JSON.stringify(that.checked));
        },
        // 全部标记已读
        All_read:function(){
            var options={};
            options.action='approvalcopyreadall';
            ns.post(options, function (succeed, data, err){
                console.log(JSON.stringify(data));
                if(succeed){
                    if(data.status){
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }

                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })

        },
        getContainer() {
            return document.querySelector('.boxarea');
        },
        choose_type:function(item){
            var that=this;
            that.chooseTypeid=item.Type;
            that.form.type=item.Type;
            that.show=false;
            that.get_data();
        },

        select_status: function (status) {
            var that = this;
            that.form.pageindex=0;
            that.form.status = status;
            that.list = [];
            that.get_data();
            that.get_menulist();
        },
        do_search: function() {
            this.get_data();
        },
        //添加审批
        add_shenpi:function(){
            var that = this;
            ns.openWin('examinetype_add', '发起审批');
        },

        openGongdan: function (ApprovalID,ItemType) {
            var that = this;
            if(ItemType==18||ItemType==19||ItemType==20){
                ns.openWin('examine_detail_hetong', '合同详情', {
                    ApprovalID: ApprovalID,
                    Type:ItemType
                });

            }else if(ItemType==21||ItemType==22||ItemType==23||ItemType==24||ItemType==25||ItemType==26||ItemType==27||ItemType==28||ItemType==29||ItemType==30){
                ns.openWin('examine_detail_yg_bangong', '审批详情', {
                    ApprovalID: ApprovalID,
                    Type:ItemType
                });
            }
            else{
                ns.openWin('examine_detail', '审批详情', {
                    ApprovalID: ApprovalID,
                    Type:ItemType
                });

            }
                
        },
        //同意
        do_open_sure:function(ApprovalID){
            var that = this;
            var options = {};
            options.ApprovalID = ApprovalID;
            options.Type=1;
            options.action = "getisoperate";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        ns.openWin('examine_yes', '审批建议', {
                            IsReject: 0,
                            ApprovalID: ApprovalID,
                        });
                    } else {
                        api.toast({
                            msg: '状态已改变，请重新选择',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        select_allstatus:function(){
            var that=this;
            that.showsearch=!that.showsearch;
        },
        doHideSearch: function () {
            var that = this;
            that.showsearch = false;
        },
        //拒绝
        do_open_jujue:function(ApprovalID){
            var that = this;
            var options = {};
            options.ApprovalID = ApprovalID;
            options.Type=2;
            options.action = "getisoperate";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        ns.openWin('examine_yes', '审批建议', {
                            IsReject: 1,
                            ApprovalID: ApprovalID,
                        });
                    } else {
                        api.toast({
                            msg: '状态已改变，请重新选择',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });            
        },
        //撤销
        doRevoke:function(ApprovalID){
            var that=this;
            var options = {};
            options.ApprovalID = ApprovalID;  
            options.action = "approvalrevoke";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        api.toast({
                            msg: '撤销成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    } else if (err) {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
    }
});

apiready = function () {

    api.parseTapmode();

    ns = window.Foresight.Util;

    dialog = new auiDialog();

    toast = new auiToast();

    app.form.servicetype = api.pageParam.servicetype || 1;
 
    app.form.RoomID = api.pageParam.id || 0;

    app.form.isviewdetail = api.pageParam.isviewdetail || false;

    if (app.form.isviewdetail) {
        app.form.status = 4;
    }

    app.form.onlybaoshi = api.pageParam.onlybaoshi || 0;


    ns.init_pull();

    ns.init_search();

    app.get_data();
    app.get_menulist();

    api.addEventListener({
        name: 'reloadexaminelist'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    api.addEventListener({
        name: 'customerservicelist_frm'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    api.addEventListener({
        name: 'is_hidden_right',
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });

    // api.addEventListener({
    //     name: 'do_open_service_jindu'
    // }, function(ret, err) {
    //     app.doOpenServiceProcess();
    // });
};
